import 'package:flustars_flutter3/flustars_flutter3.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:my_shop/cache_event_identification.dart';
import 'package:my_shop/load_my_goods.dart';
import 'package:my_shop/my/login_page.dart';
import 'package:my_shop/my/register_page.dart';
import 'package:my_shop/my/setting_page.dart';

class MyPage extends StatefulWidget {
  const MyPage({
    super.key,
  });

  @override
  State<MyPage> createState() => _MyPageState();
}

class _MyPageState extends State<MyPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SingleChildScrollView(
        child: Column(
          children: [
            Container(
              height: 160,
              decoration: BoxDecoration(
                gradient: LinearGradient(
                  begin: Alignment.topCenter,
                  end: Alignment.bottomCenter,
                  colors: [
                    Color(((SpUtil.getInt(Constant.mainColor) ??
                                    MyColor.mainColor.value) +
                                2000) <
                            0
                        ? (SpUtil.getInt(Constant.mainColor) ??
                            MyColor.mainColor.value)
                        : ((SpUtil.getInt(Constant.mainColor) ??
                                MyColor.mainColor.value) +
                            2000)),
                    Color(SpUtil.getInt(Constant.mainColor) ??
                        MyColor.mainColor.value),
                  ],
                ),
              ),
              child: _buildTopPersonalBasicInfo(),
            ),
            const SizedBox(
              height: 10,
            ),
            _everyJumpColumn('Register', () {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (context) => const RegisterPage(),
                ),
              );
            }),
            const SizedBox(
              height: 5,
            ),
            _everyJumpColumn('Log in', () {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (context) => const LoginPage(),
                ),
              ).then((value) => setState(() {}));
            }),
            const SizedBox(
              height: 5,
            ),
            _everyJumpColumn('Log out', () {
              Fluttertoast.showToast(msg: 'Log out success');
              setState(() {
                SpUtil.putBool(Constant.isLoginStatus, false);
                SpUtil.putString(Constant.isLoginStatusUserName, '');
              });
            }),
            const SizedBox(
              height: 5,
            ),
            _everyJumpColumn('Clear cache', () {
              Fluttertoast.showToast(
                  msg:
                      'Clear cache success，all data has been restored to the original state');
              setState(() {
                SpUtil.putBool(Constant.isLoginStatus, false);
                SpUtil.putString(Constant.isLoginStatusUserName, '');
                SpUtil.putObjectList(Constant.cartModelList, []);
                SpUtil.putObjectList(
                    Constant.allGoodsList, MyAllGoods.allGoodsList);
                SpUtil.putObjectList(Constant.userModelList, []);
                SpUtil.putObjectList(Constant.orderList, []);
                SpUtil.putInt(Constant.mainColor, MyColor.mainColor.value);
              });
            }),
            const SizedBox(
              height: 5,
            ),
            _everyJumpColumn('Setting', () {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (context) => const SettingPage(),
                ),
              ).then((value) => setState(() {}));
            }),
          ],
        ),
      ),
    );
  }

  Widget _buildTopPersonalBasicInfo() {
    return Container(
      padding: const EdgeInsets.all(16),
      child: Row(
        children: [
          Expanded(
            child: Column(
              children: [
                const SizedBox(
                  height: 50,
                  child: Row(
                    children: [
                      Text(
                        'Me',
                        style: TextStyle(
                            fontSize: 18, fontWeight: FontWeight.bold),
                      ),
                    ],
                  ),
                ),
                // Gaps.vGap20,
                Row(
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: [
                    Row(
                      children: [
                        ClipRRect(
                            borderRadius: BorderRadius.circular(34),
                            child: Image.asset(
                              MyAllGoods.allGoodsList.first.localUrl ?? '',
                              width: 56,
                              height: 56,
                            )),
                        Container(
                          margin: const EdgeInsets.only(left: 12),
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.center,
                            children: [
                              Container(
                                decoration: BoxDecoration(
                                  color: const Color(0xFFF8961E),
                                  borderRadius: BorderRadius.circular(19),
                                ),
                                padding: const EdgeInsets.only(
                                  left: 10,
                                  right: 10,
                                ),
                                child: Text(
                                  SpUtil.getString(
                                          Constant.isLoginStatusUserName) ??
                                      '',
                                  style: const TextStyle(
                                    fontSize: 14,
                                    fontWeight: FontWeight.bold,
                                    color: Colors.white,
                                  ),
                                ),
                              ),
                            ],
                          ),
                        ),
                      ],
                    ),
                  ],
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  /// One-line jump
  Widget _everyJumpColumn(String title, VoidCallback voidCallBack) {
    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: () {
        voidCallBack.call();
      },
      child: Container(
        margin: const EdgeInsets.only(left: 10, right: 10),
        padding: const EdgeInsets.all(16),
        decoration: BoxDecoration(
          color: Color(
              SpUtil.getInt(Constant.mainColor) ?? MyColor.mainColor.value),
          borderRadius: BorderRadius.circular(8.0),
        ),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Text(title),
            Image.asset(
              'assets/images/home/ic_arrow_right.png',
              width: 14,
              height: 14,
            ),
          ],
        ),
      ),
    );
  }
}
